package com.li.service.Impl;

import com.li.DTO.LoginDTO;
import com.li.mapper.CompanyMapper;
import com.li.mapper.EmployeeMapper;
import com.li.mapper.LoginMapper;
import com.li.pojo.Company;
import com.li.pojo.Employee;
import com.li.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zhiqingLi
 * @Description:
 * @Date: Created in 8:48 2024/3/13
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    //登录
    @Override
    public Employee login(LoginDTO loginDTO) {
        Employee employee = loginMapper.login(loginDTO);
        if (employee != null){
            return employee;
        }
        throw new RuntimeException("账号或密码不正确");
    }

    //注册
    @Override
    public Integer enrol(Company company) {
        Employee employee = new Employee();
        Company company1 = new Company();
        //注册公司的那个 是系统管理员 也是该公司的员工
        BeanUtils.copyProperties(company,company1);
        //数据库里添加操作
        Integer save = companyMapper.save(company1);
        BeanUtils.copyProperties(company,employee);
        employee.setCompanyId(company1.getId());
        employee.setName(company1.getCompanyName());
        employee.setRole("系统管理员");
        //0是激活 1是禁用
        employee.setIsf(0);
        return employeeMapper.save(employee);
    }
}
